local injectHome = {}
_G.injectHome = injectHome


local addOnName = "injectHome"
local addOnVersion = "1.3"
local addOnAuthor = "NashaRiochtDE"

local skillTab, skillPage, buttonId, buttonIdTotal = 1, 1, 2, 2

local stringToFind = "recall"
if GetLanguage() == "DE" then
	stringToFind = "ckruf"
end

local pairs = pairs
local stringfind = string.find
local stringlower = string.lower


function injectHome_OnLoad(this)
	this:RegisterEvent("VARIABLES_LOADED")
	this:RegisterEvent("LOADING_END")
	this:RegisterEvent("CHAT_MSG_SYSTEM")
	this:RegisterEvent("SAVE_VARIABLES")
	SlashCmdList["injectHome"] = injectHome_SlashCmdHandler
	_G["SLASH_injectHome1"] = "/injecthome"
	SaveVariables("injectHomeSave")
end

function injectHome_SlashCmdHandler(ebox, msg)
	if msg == "info" then
		DEFAULT_CHAT_FRAME:AddMessage("> "..addOnName..": V"..addOnVersion.." by "..addOnAuthor)
	elseif msg == "status" then
		DEFAULT_CHAT_FRAME:AddMessage(UnitName("player")..": "..injectHome[UnitName("player")])
	else
		DEFAULT_CHAT_FRAME:AddMessage("> "..addOnName..":")
		DEFAULT_CHAT_FRAME:AddMessage(" /info (version+author)")
		DEFAULT_CHAT_FRAME:AddMessage(" /status (saved location)")
	end
end

function injectHome_OnEvent(event, arg1)
	if event == "VARIABLES_LOADED" then
		injectHome = injectHomeSave
		if injectHome == nil then
			injectHome = {}
		end
		DEFAULT_CHAT_FRAME:AddMessage("> "..addOnName.." loaded!")
		DEFAULT_CHAT_FRAME:AddMessage("/"..addOnName)
		return
	end
	if event == "LOADING_END" then
		--hook skillbook icontt
		function OnEnter_SkillItemButton(frame, ButtonID)
			local iIndex = (gSkillFrame.page - 1) * DF_MAXPAGESKILL_SKILLBOOK + ButtonID
			GameTooltip:SetOwner(frame:GetParent(), "ANCHOR_RIGHT", 10, 0)
			GameTooltip:SetSkillItem(gSkillFrame.type, iIndex)
			if gSkillFrame.type == skillTab and gSkillFrame.page == skillPage and ButtonID == buttonId and iIndex == buttonIdTotal then
				GameTooltip:AddLine(injectHome[UnitName("player")])
			end
			gSkillFrame.index = iIndex
		end
		--hook skillbook skillfieldtt
		function OnEnter_SkillButton(frame, ButtonID)
			local iIndex = (gSkillFrame.page - 1) * DF_MAXPAGESKILL_SKILLBOOK + ButtonID
			local SkillButton = frame:GetParent()
			if SkillButton.EnableToLV == 1 then	
				if gSkillFrame.SelectID ~= iIndex and gSkillFrame.type ~= 3 then
					getglobal(frame:GetName().."Highlight"):Show()
				end	
			end
			GameTooltip:ClearAllAnchors()
			GameTooltip:SetOwner(frame, "ANCHOR_RIGHT", 10, 0)
			GameTooltip:SetSkillItem(gSkillFrame.type, iIndex)
			if gSkillFrame.type == skillTab and gSkillFrame.page == skillPage and ButtonID == buttonId and iIndex == buttonIdTotal then
				GameTooltip:AddLine(injectHome[UnitName("player")])
			end
			gSkillFrame.index = iIndex
		end
		--hook actionbartt
		local barPrefix = {"Main", "Bottom", "Right", "Left"}
		for _, v in pairs(barPrefix) do
			for i = 1, ACTIONBAR_NUM_BUTTONS, 1 do 
				local actionButton = getglobal(v.."ActionBarFrameButton"..i)
				actionButton.__uiLuaOnMouseEnter__ = injectHome_ActionButton_OnMouseEnter
			end
		end
		return
	end
	if event == "CHAT_MSG_SYSTEM" then
		local theString = arg1
		if stringfind(theString, stringToFind) ~= nil then
			injectHome[UnitName("player")] = theString
		end	
		return
	end
	if event == "SAVE_VARIABLES" then
		injectHomeSave = injectHome
	end
end

--look for stringToFind in the upperleft field of the actionbartt
function injectHome_ActionButton_OnMouseEnter(button)
	ActionButton_OnEnter(button)
	if HasAction(ActionButton_GetButtonID(button)) then
		GameTooltip:SetOwner(button, "ANCHOR_TOPLEFT", -5, 0)
		local headlineTxt = getglobal("GameTooltipTextLeft1"):GetText()
		if stringfind(stringlower(headlineTxt), stringToFind) ~= nil then
			GameTooltip:AddLine(injectHome[UnitName("player")])
		end
		GameTooltip:Show()
	else
		GameTooltip:Hide()
	end
end
